package PlotPourri.Database;

import PlotPourri.*;
import PlotPourri.Band;
import PlotPourri.BandName;
import PlotPourri.Observation;
import PlotPourri.Star;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class DatabaseLMCjhk extends StarDatabase {

    Map<Integer, Star> differentPeriods;

    public DatabaseLMCjhk(File main, File dataDirectory) {
        super(main, dataDirectory);
        System.out.print("Loading, please wait...");
        File[] listOfFiles = dataDirectory.listFiles();
        Star star = null;
        Map<Integer, Star> stars = new HashMap<Integer, Star>();
        for (int i = 0; i < listOfFiles.length; i++) {
            if (i % 500 == 0) System.out.print(".");
            String full = listOfFiles[i].toString();
            if (full.split("_").length < 6) continue;
            int name = Integer.parseInt(full.split("_")[2]);
            if (stars.containsKey(name)) star = stars.get(name);
            else star = new Star(BandName.getJHK(), name);
            if (star != null) {
                BandName band = BandName.J;
                for (BandName bandName : BandName.getJHK()) if (bandName.toString().equalsIgnoreCase(full.split("_")[full.split("_").length - 1].toUpperCase().charAt(0) + "")) {
                        band = bandName;
                        break;
                    }
                Observation datfile = new Observation(full, Integer.parseInt(full.split("_")[full.split("_").length - 2]));
                datfile.setPhasePosition(0);
                datfile.setMagnitudePosition(1);
                datfile.setErrorPosition(2);
                datfile.setJulianDatePosition(3);
                star.getBand(band).add(datfile);
                stars.put(name, star);
            }
        }
        Scanner sc = null;
        try {
            sc = new Scanner(main);
        } catch (FileNotFoundException ex) {
            System.err.print(ex + "\n");
        }
        if (sc != null) {
            differentPeriods = new HashMap<Integer, Star>();
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                while (line.startsWith(" ")) line = line.substring(1);
                if (line.startsWith("#CF") || Integer.parseInt(line.split("\\s+")[1]) >= 5000000 || Integer.parseInt(line.split("\\s+")[2]) == 0) continue;
                star = stars.get(Integer.parseInt(line.split("\\s+")[4]));
                double period = Double.parseDouble(line.split("\\s+")[9]);
                star.setPeriod(period);
                for (Band band : star.getBands()) for (Observation file : band) if (file.getPeriod() != period) {
                            differentPeriods.put(star.getName(), star);
                            break;
                        }
                star.setCl(Star.PulsationMode.valueOf(line.split("\\s+")[23]));
                Observation datfile = star.getDatfile(Integer.parseInt(line.split("\\s+")[1]), star.getBand(BandName.J));
                datfile.setPeriod(period);
                datfile.setDistance(Math.sqrt(Math.pow(Double.parseDouble(line.split("\\s+")[19]) + Double.parseDouble(line.split("\\s+")[20]), 2)));
                datfile = star.getDatfile(Integer.parseInt(line.split("\\s+")[2]), star.getBand(BandName.K));
                datfile.setPeriod(period);
                datfile.setDistance(Math.sqrt(Math.pow(Double.parseDouble(line.split("\\s+")[21]) + Double.parseDouble(line.split("\\s+")[22]), 2)));
            }
            for (Star s : stars.values()) put(s.getName(), s);
            sc.close();
            System.out.println(" Done!");
        }
    }
}
